﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

namespace UIFramework
{
    /// <summary>
    /// UI窗体之间收发消息管理
    /// </summary>
    public class UIMsgManager : MonoBehaviour
    {
        #region 将本脚本设置为单例模式
        private static UIMsgManager _instance;
        private UIMsgManager() { }
        public static UIMsgManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = GameInit.GameManager.AddComponent<UIMsgManager>();
                }
                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// 存放UI窗体注册的消息监听事件
        /// </summary>
        public Dictionary<string, List<Action<object, string>>> _AllListenerDic = new Dictionary<string, List<Action<object, string>>>();

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            //Debug.Log("UIMsgManager Init!"); 
        }

        /// <summary>
        /// 注册(添加)监听事件,一个消息名在不同面板添加多个方法
        /// 相当于多播委托
        /// </summary>
        /// <param name="msgName"></param>
        /// <param name="needOperation"></param>
        public void AddListener(string msgName, Action<object, string> needOperation)
        {
            if (_AllListenerDic.ContainsKey(msgName) && _AllListenerDic[msgName].Contains(needOperation))
            {
                Debug.LogError("该操作已经被监听过了,不能重复添加!");
            }
            else
            {
                if (!_AllListenerDic.ContainsKey(msgName))
                {
                    _AllListenerDic.Add(msgName, new List<Action<object, string>>() { needOperation });
                }
                else
                {
                    _AllListenerDic[msgName].Add(needOperation);
                }
                //Debug.Log(msgName + " 监听添加成功!");
            }
        }
        /// <summary>
        /// 移除某个监听事件 事件名➕方法名
        /// </summary>
        /// <param name="msgName"></param>
        /// <param name="op"></param>
        public void RemoveListener(string msgName, Action<object, string> op)
        {
            var keys = _AllListenerDic.Keys;
            foreach (var key in keys)
            {
                if (msgName == key)
                {
                    _AllListenerDic[key].Remove(op);
                    if (_AllListenerDic[key].Count == 0)
                    {
                        _AllListenerDic.Remove(key);
                    }
                    //CommonTools.Debug("移除事件:" + key + " " + op.ToString());
                    break;
                }
            }
        }
        /// <summary>
        /// 移除某条监听事件 事件名
        /// </summary>
        /// <param name="msgName"></param>
        /// <param name="op"></param>
        public void RemoveOneLineListener(string msgName)
        {
            var keys = _AllListenerDic.Keys;
            foreach (var key in keys)
            {
                if (msgName == key)
                {
                    _AllListenerDic.Remove(key);
                    break;
                }
            }
        }
        /// <summary>
        /// 移除所有监听事件
        /// </summary>
        public void RemoveAllListener()
        {
            var allForm = UIManager.Instance._DicAllFormCache;
            foreach (var form in allForm.Values)
            {
                form.RemoveAllUIMsgListener();
            }
            //Debug.Log(_AllListenerDic.Count);
            _AllListenerDic.Clear();
            _AllListenerDic = new Dictionary<string, List<Action<object, string>>>();
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msgName"></param>
        /// <param name="needOperation"></param>
        public void SendUIMssage(string msgName, object data, string sendUIName = "Unkonw")
        {
            foreach (var key in _AllListenerDic.Keys)
            {
                if (key == msgName)
                {
                    foreach (var item in _AllListenerDic[key])
                    {
                        item.Invoke(data, sendUIName);
                    }
                    break;
                }
            }
        }

    }
}